import { Toast } from "antd-mobile"
import { LangThai } from "./langThai"


/**
 * 判断对象是否为字符串
 * @param {Object} obj
 */
export const isString = obj => {
    return (typeof obj === "string")
}

/**
 * 判断对象是否为数组
 * @param {Object} obj
 */
export const isArray = obj => {
    return (Array.isArray(obj))
}

/**
 * 判断对象是否为空
 * @param {Object} obj
 */
export const isEmptyObject = obj => {
    for (let pro in obj) { return false }
    return true
}

/**
 * 判断对象是否为null
 * @param {Object} obj
 */
export const isNullObject = obj => {
    return !obj
}

/**
 * 判断字符串是否为空
 * @param {String} obj
 */
export const isEmptyString = obj => {
    if (isNullObject(obj)) {
        return true
    }
    if (!isString(obj)) {
        return true
    }
    if (obj.length <= 0) {
        return true
    }
    return false
}

/**
 * 判断字符串是否为必填
 * @param {String} obj
 */
export const isMustProofString = obj => {
    if (isNullObject(obj)) {
        return true
    }
    if (!isString(obj)) {
        return true
    }
    if (obj.length <= 0) {
        return true
    }
    if (obj === "YES") {
        return true
    }
    return false
}

/**
 * 判断字符串是否为邮箱格式
 * @param {String} obj
 */
export const isEmailValid = obj => {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(obj);
}

/**
 * 判断状态是否为成功， 以 SUCCESS 结尾
 * @param {String} obj
 */
export const isStatusSuccess = obj => {
    if (!isString(obj)) {
        return false
    }
    const flag = "SUCCESS"
    const temp = obj + ""
    return (temp.endsWith(flag))
}

/**
 * 返回安全字符串
 * @param {String} obj
 */
export const toSafeString = obj => {
    if (isNullObject(obj)) {
        return ""
    }
    return obj
}

/**
 * 返回字符串列表，以指定格式分割
 * @param {String} obj
 */
export const toList = (obj, split) => {
    if (isNullObject(obj)) {
        return []
    }
    const splitFormat = isEmptyString(split) ? ":" : split
    return obj.split(splitFormat)
}

/**
 * 返回百分比字符串
 * @param {String} obj
 */
export const toPercentageString = (obj, fixed = 0) => {
    if (isNullObject(obj)) {
        return ""
    }
    const floatNumber = parseFloat(obj);
    const percentage = (floatNumber * 100).toFixed(fixed);
    return (percentage + "%")
}

/**
 * 返回地址加密字符串
 * @param {String} obj
 */
export const toEllipsisWalletAddress = (obj) => {
    if (isNullObject(obj)) {
        return ""
    }

    if (obj.length >= 20) {
        let start = obj.substring(0, 10)
        let end = obj.substring(obj.length - 10, obj.length)
        return (start + "..." + end);
    }

    return obj;
}

/**
 * 返回去掉首尾空串的字符串
 * @param {String} obj
 */
export const toTrimString = (obj) => {
    if (isString(obj)) {
        const trimStr = obj.replace(/^\s+|\s+$/g, "");
        return trimStr
    }
    return obj
}

/**
 * 返回当前语言
 */
export const checkLanguageByTimeZone = () => {
    // 测试
    if (true) {
        return "thai"
    }

    if (isCountryNrly()) {
        return "us"

    }
    if (isCountryThai()) {
        return "thai"
    }
    return "vietnam"
}

/**
 * 返回当前国家
 */
export const checkCountryByTimeZone = () => {
    // 测试
    if (true) {
        return "thai"
    }

    if (isCountryNrly()) {
        return "nrly"

    }
    if (isCountryThai()) {
        return "thai"
    }
    return "vietnam"
}

/**
 * 判断是否为泰国
 */
export const isCountryThai = () => {
    // 测试
    if (true) {
        return true;
    }

    let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    if (timeZone === "Asia/Bangkok") {
        return true

    } else {
        return false
    }
}

/**
 * 判断是否为越南
 */
export const isCountryVnd = () => {
    // 测试
    if (true) {
        return false;
    }

    let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    if (timeZone === "Africa/Lagos") {
        return false

    } else if (timeZone === "Asia/Bangkok") {
        return false

    } else {
        return true
    }
}

/**
 * 判断是否为尼日利亚
 */
export const isCountryNrly = () => {
    // 测试
    if (true) {
        return false;
    }

    // 测试
    if (false) {
        let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
        if (timeZone === "Africa/Lagos") {
            return true

        } else {
            return false
        }
    }

    return false;
}

/**
 * 返回当前系统类型
 * @returns {String} 系统类型
 */
export const checkOSType = () => {
    const userAgent = navigator.userAgent.toLowerCase()
    console.log("userAgent: " + userAgent)

    if (userAgent.indexOf("android") !== -1) {
        // 这是Android设备
        return "ANDROID"

    } else if ((userAgent.indexOf("iphone") !== -1) ||
        (userAgent.indexOf("ipad") !== -1) ||
        (userAgent.indexOf("ipod") !== -1) ||
        (userAgent.indexOf("ios") !== -1)) {
        // 这是iOS设备
        return "IOS"

    } else {
        // 这不是Android也不是iOS设备，可能是其他设备或桌面浏览器
        return "OTHER"
    }

    //如何正则

    // if (/(android|Android)/.test(userAgent)) {
    //     // 这是Android设备
    //     return "ANDROID"
    // } else if (/(iPhone|iPad|iPod|iOS)/.test(userAgent)) {
    //     // 这是iOS设备
    //     return "IOS"

    // } else {
    //     // 这不是Android也不是iOS设备，可能是其他设备或桌面浏览器
    //     return "OTHER"
    // }
}

/**
 * 复制文本
 */
export const toCopyText = (text) => {
    if (navigator.clipboard && navigator.permissions) {
        navigator.clipboard.writeText(text)
            .then(function () {
                console.log('Successfully!');
                Toast.show(LangThai.copySuccess)
            })
            .catch(function (err) {
                try {
                    const textArea = document.createElement('textArea')
                    textArea.value = text
                    textArea.style.width = 0
                    textArea.style.position = 'fixed'
                    textArea.style.left = '-999px'
                    textArea.style.top = '10px'
                    textArea.setAttribute('readonly', 'readonly')
                    document.body.appendChild(textArea)
                    textArea.focus()
                    textArea.select()
                    document.execCommand('copy')
                    document.body.removeChild(textArea)

                    console.log('Successfully!');
                    Toast.show(LangThai.copySuccess)
                }
                catch (error) {
                    Toast.show(error.toString() || "");
                }
            });
    } else {
        try {
            const textArea = document.createElement('textArea')
            textArea.value = text
            textArea.style.width = 0
            textArea.style.position = 'fixed'
            textArea.style.left = '-999px'
            textArea.style.top = '10px'
            textArea.setAttribute('readonly', 'readonly')
            document.body.appendChild(textArea)
            textArea.focus()
            textArea.select()
            document.execCommand('copy')
            document.body.removeChild(textArea)

            console.log('Successfully!');
            Toast.show(LangThai.copySuccess)
        }
        catch (error) {
            Toast.show(error.toString() || "");
        }
    }
}
